#include "datasviewer.h"
#include <common/constants.h>
#include <QSqlTableModel>
#include <QDate>
#include <QSqlError>
#include <QDebug>

using namespace Constants;

DatasViewer::DatasViewer(QObject *parent,const QString uuid)
{
    Q_UNUSED(parent);    
    setupUi(this);
    setWindowModality(Qt::WindowModal);
    setWindowFlags(Qt::Dialog);
    m_uuid = uuid;
    m_db = QSqlDatabase::database(DATABASE_BMISURVEY);
    m_model= new QSqlTableModel(this,m_db);
    m_model->setTable(WEIGHT_HEIGHT_TABLE);
    m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    QString filter = QString("%1 = '%2'").arg("patient_uuid",m_uuid);
    m_model->setFilter(filter);
    m_model->select();
    connect(addButton,SIGNAL(pressed()),this,SLOT(addRowOfDatas()));
    connect(deleteButton,SIGNAL(pressed()),this,SLOT(deleteRowOfDatas()));
    connect(quitButton,SIGNAL(pressed()),this,SLOT(close()));
    connect(applyButton,SIGNAL(pressed()),this,SLOT(applyAndSubmit()));
    
}

DatasViewer::~DatasViewer(){}

void DatasViewer::addRowOfDatas()
{
    int newRow = m_model->rowCount();
    int lastId = WHTableLastId();
    int newId = lastId +1;
    QString weight;
    QString height;
    QString dateString =  QDate::currentDate().toString("yyyy-MM-dd");
    m_model->insertRows(newRow,1);    
    qDebug() << __FILE__ << QString::number(__LINE__) << " newRow =" << QString::number(newRow) ;
    m_model->setData(m_model->index(newRow,ID_WH),newId,Qt::EditRole);
    m_model->setData(m_model->index(newRow,WH_UUID),m_uuid,Qt::EditRole);
    m_model->setData(m_model->index(newRow,WH_WEIGHT),weight,Qt::EditRole);
    m_model->setData(m_model->index(newRow,WH_HEIGHT),height,Qt::EditRole);    
    m_model->setData(m_model->index(newRow,WH_INPUT_DATE),dateString,Qt::EditRole);
    if (!m_model->submitAll())
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "UNABLE TO ADD ROW"
    	  << m_model->lastError().text() ;
        }
    showView(m_model);    
}

void DatasViewer::deleteRowOfDatas()
{
    QModelIndex index = tableView->currentIndex();
    int row = index.row();
    m_model->removeRows(row,1);   
    m_model->submit(); 
}

void DatasViewer::applyAndSubmit()
{
    qDebug() << __FILE__ << QString::number(__LINE__) << " rows applyAn =" << QString::number(m_model->rowCount()) ;
    if (!m_model->submitAll())
    {
    	 qWarning() << __FILE__ << QString::number(__LINE__) << "UNABLE TO SUBMIT VALUES" ; 
        }
    showView(m_model);
}

void DatasViewer::showDatas()
{
    QSqlTableModel *model = new QSqlTableModel(this,m_db);
    model->setTable(WEIGHT_HEIGHT_TABLE);
    QString filter = QString("%1 = '%2'").arg("patient_uuid",m_uuid);
    model->setFilter(filter);
    model->select();
    showView(model);
    show();
    qDebug() << __FILE__ << QString::number(__LINE__) << " rows =" << QString::number(model->rowCount()) ;
}

void DatasViewer::showView(QSqlTableModel * model)
{
    model->setHeaderData(WH_WEIGHT, Qt::Horizontal, tr("Weight"));
    model->setHeaderData(WH_HEIGHT, Qt::Horizontal, tr("Height"));
    model->setHeaderData(WH_INPUT_DATE, Qt::Horizontal, tr("Date"));
    tableView->setModel(model);
    tableView->hideColumn(ID_WH);
    tableView->hideColumn(UUID);  
    tableView->horizontalHeader()->resizeSections(QHeaderView::Interactive);
    tableView->horizontalHeader()->setStretchLastSection (true) ;
}

int DatasViewer::WHTableLastId()
{
    QSqlTableModel model(this,m_db);
    model.setTable(WEIGHT_HEIGHT_TABLE);
    model.select();
    int rows = model.rowCount();
    return model.data(model.index(rows-1,ID_WH),Qt::DisplayRole).toInt();
}

